package com.zjj.learn.designmodel.work.mediator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 具体中介者
 **/
public class MediatorObject implements Mediator {

    private List<HouseOwner> houseOwners = new ArrayList<>();
    private List<Tenant> tenants = new ArrayList<>();

    public MediatorObject() {
    }

    /**
     * 向中介注册房东信息
     **/
    public void setHouseOwners(HouseOwner... houseOwner) {
        this.houseOwners.addAll(Arrays.asList(houseOwner));
    }

    /**
     * 向中介注册租客信息
     **/
    public void setTenants(Tenant... tenant) {
        this.tenants.addAll(Arrays.asList(tenant));
    }

    /**
     * 中介发布房屋信息时，就向所有的租客传递房源信息
     **/
    @Override
    public void publicHouse(String message) {
        if (!this.tenants.isEmpty()) {
            for (Tenant tenant : this.tenants) {
                tenant.contact(message);
            }
        }
    }

    /**
     * 当有人需要房子时，中介就向所有的房东传递信息
     **/
    @Override
    public void needHouse(String message) {
        if (!this.houseOwners.isEmpty()) {
            for (HouseOwner owner : this.houseOwners) {
                owner.contact(message);
            }
        }
    }
}
